#!c:/usr/perl

##########################
#     OmnibusProxy       #
#    Krzysztof Ulman     #
# Politechnika Krakowska #
#      Kraków 2012       #
##########################

use strict;
use warnings;
use encoding 'utf8';

# 1 - google
# 2 - netsprint
# 3 - bing
# 4 - altavista
my %map;
$map{'google'} = 1;
$map{'netsprint'} = 2;
$map{'bing'} = 3;
$map{'altavista'} = 4;

our $ID = $map{$ARGV[0]};
our $LIMIT = int($ARGV[1]);
if(!$ID) {
	die('Brak ID');
}
print "LIMIT: $LIMIT\n";

open(FILE, "additional.log") || die('Nie mozna otworzyc pliku');
open(FILE_OUT, ">out.txt");

my $base;
my $prevRes = 0; 
my $counter = 0;
my $regExp = "ID\:$ID";
while(my $line = <FILE>) {
	if(!$base) {
		$line =~ m/TIME\:(\d+)/;
		my $res = int($1);
		$base = $res;
		$prevRes = $base;
	}
	if($counter < $LIMIT) {
		chomp $line;
		if($line =~ m/$regExp/) {
			$line =~ m/TIME\:(\d+)/;
			my $res = int($1);
			while($prevRes < ($res - 1) && $counter < $LIMIT) {
				$prevRes++;
				my $prevValue = $prevRes - $base;
				print FILE_OUT "$prevValue\t0\n";
				$counter++;
			}
			if($counter < $LIMIT) {
				my $value = $res - $base;
				my $sign = 1;
				if($line =~ m/REPLACEMENT\:1/) {
					$sign++;
				}
				print FILE_OUT "$value\t" . "$sign\n";
				$counter++;
				
				$prevRes = $res;
			}
		}
	}
}

close(FILE_OUT);
close(FILE);

print "KONIEC\n";